package cn.life.business.notice.dao.impl;

import cn.life.business.notice.entity.po.NoticeUserPo;
import cn.life.business.user.entity.po.UserInfo;
import cn.life.business.warehouse.entity.po.WarehouseDetails;
import cn.life.common.entity.model.MyUserInfo;
import cn.life.common.entity.vo.Pagination;
import cn.life.common.support.jdbc.AbstractSupportDao;
import cn.life.business.notice.dao.NoticeDao;
import cn.life.business.notice.entity.po.Notice;
import cn.life.business.notice.entity.vo.NoticeVo;
import org.springframework.stereotype.Repository;

import java.awt.print.Pageable;
import java.util.ArrayList;
import java.util.List;

/**
 * @author wangzhenyu
 * @Date 2018/7/6
 */
@Repository
public class NoticeDaoImpl  extends AbstractSupportDao implements NoticeDao {

    @Override
    public Long addNotice(Notice notice) {
        Long noticeId = queryTableNextId("factory_notice","notice_id");
        String sql="INSERT INTO factory_notice(notice_id,notice_title,notice_content,file_url,opr_user_id,opr_user_name," +
                "opr_date,project_id) VALUES(?,?,?,?,?,?,?,?)";
        return update(sql,noticeId,notice.getNoticeTitle(),notice.getNoticeContent(),notice.getFileUrl()
                ,notice.getOprUserId(),notice.getOprUserName(),notice.getOprDate(),notice.getProjectId()) == 1 ? noticeId : 0;
    }



    @Override
    public int deleteNotice(long noticeId) {
        int res = deleteNoticeDetail(noticeId);
        if(res > 0){
            String sql= "delete from factory_notice where notice_id=?";
            return update(sql,noticeId);
        }
        return res;
    }

    @Override
    public int deleteNoticeDetail(long noticeId) {
        String sql= "delete from factory_notice_detail where notice_id=?";
        return update(sql,noticeId);
    }

    @Override
    public int updateNotice(Notice notice) {
        String sql= "update factory_notice set notice_title=?,notice_content=?,file_url=?,project_id=? where notice_id=?";
        return update(sql,notice.getNoticeTitle(),notice.getNoticeContent(),notice.getFileUrl(),notice.getProjectId(),notice.getNoticeId());
    }

    @Override
    public Pagination<NoticeVo> getAllNoticeInfo(NoticeVo noticeVo) {
       //String sql= "select * from factory_notice where user_ids like'%" +noticeVo.getConsumerId()+"%'";
        StringBuffer stringBuffer= new StringBuffer("select distinct a.*,(select count(1) from factory_notice_detail b where b.notice_id = a.notice_id and b.state = 1) as readNumber,(select count(1) from factory_notice_detail b where b.notice_id = a.notice_id and b.state = 0) as notReadNumber from factory_notice a,factory_notice_detail b where a.notice_id = b.notice_id and (a.opr_user_id = ? or b.user_id = ?) ");
        if(!"".equals(noticeVo.getNoticeTitle())&&noticeVo.getNoticeTitle()!=null){
           stringBuffer.append(" and a.notice_title like'%"+noticeVo.getNoticeTitle()+"%'");
        }
        if(noticeVo.getProjectId()!=null && !"".equals(noticeVo.getProjectId())){
            stringBuffer.append(" and a.project_id =" + noticeVo.getProjectId() + " ");
        }

        stringBuffer.append(" ORDER BY opr_date DESC");
        return queryForListByPageEx(stringBuffer.toString(),NoticeVo.class,noticeVo.getPageIndex(),noticeVo.getPageSize(),noticeVo.getOprUserId(),noticeVo.getOprUserId());
    }

    @Override
    public NoticeVo getNotice(NoticeVo noticeVo) {
        String sql= "select * from factory_notice where notice_id=?";
        return queryForObject(sql,NoticeVo.class,noticeVo.getNoticeId());
    }

    @Override
    public int addNoticeDetail(Notice notice) {
        String sql = "insert into factory_notice_detail(notice_id,user_id,user_name,state) values(?,?,?,?)";
        List<Object[]> list = new ArrayList<>();
        for(NoticeUserPo userInfo :notice.getUserList()){
            Object[] objects = new Object[4];
            objects[0]=notice.getNoticeId();
            objects[1]=userInfo.getUserId();
            objects[2]=userInfo.getUserName();
            objects[3]=0;
            list.add(objects);
        }
        batchUpdate(sql,list);
        return 1;
    }

    @Override
    public List<NoticeUserPo> getNoticeDetail(NoticeVo noticeVo) {
        String sql = "select * from factory_notice_detail where notice_id = ?";
        return queryForList(sql,NoticeUserPo.class,noticeVo.getNoticeId());
    }

    @Override
    public int updateNoticeState(NoticeUserPo noticeUserPo) {
        String sql="update factory_notice_detail set state=? where detail_id=?";
        return update(sql,noticeUserPo.getState(),noticeUserPo.getDetailId());
    }

    @Override
    public void updateNoticeReadState(NoticeVo noticeVo, MyUserInfo myUserInfo) {
        String sql = "update factory_notice_detail set state = 1 where notice_id = ? and user_id = ?";
        update(sql,noticeVo.getNoticeId(),myUserInfo.getUserId());
    }

    @Override
    public Integer queryMyNotReadNumber(Long projectId, Long userId) {
        String sql="select count(1) from factory_notice a inner join factory_notice_detail b on a.notice_id = b.notice_id where a.project_id = ? and b.user_id = ? and state = 0";
        return queryForObject(sql,Integer.class,projectId,userId) > 0 ? 1 : 0;
    }
}
